<?php
class Zend_View_Helper_CheckBoxList
{
	public function checkBoxList($list, $field, $name = null, $id = null, $selected = array())
	{
		echo "<!-- $name -->\n";
		echo $this->createOption($list, $field, $name, $id, 0, null, $selected);
		echo "<!-- End $name -->\n";
	}
	
	private function createOption($list, $field, $name = null, $id = null, $parentId, $space = null, $selected = array())
	{
		$tmp = null;
		foreach ($list as $item) {
			if($item['parent_id'] == $parentId) {
				if(in_array($item[$field[0]], $selected)) {
					$more = "checked";
				} else {
					$more = null;
				}
				$idx = null;
				if($id != null) {
					$idx = "id=\"{$id}-{$item[$field[0]]}\"";
				}
				$tmp .= "<li><label><input type=\"checkbox\" name=\"{$name}\" {$idx} value=\"{$item[$field[0]]}\" {$more} /><span>".$item[$field[1]]."</span></label>";
				$tmp .= $this->createOption($list, $field, $name, $id, $item[$field[0]], $space.'--', $selected);
				$tmp .= "</li>\n";
			}
		}
		// Insert ul tag
		$html = null;
		if($tmp != null) {
			if($space == null) {
				$class="categorychecklist";
			} else {
				$class="children";
			}	
			$html = "<ul class=\"{$class}\">";
			$html .= $tmp;
			$html .= "</ul>";
		}
		return $html;
	}
}